﻿using System;
using System.Linq.Expressions;
using Fuse8.DomainFramework.Domain;

namespace Fuse8.DomainFramework.DataAccess.SqlDatabase.Formatters
{
	internal class BooleanExpressionFormattingService : IFormattingService
	{
		public object FormatObject(object obj, ExpressionType? parentExpressionType)
		{
			if (parentExpressionType == ExpressionType.Call || parentExpressionType == ExpressionType.Equal || parentExpressionType == ExpressionType.NotEqual)
			{
				return string.Format("(CASE WHEN {0} THEN 1 WHEN NOT ({0}) THEN 0 ELSE NULL END)", obj);
			}

			return obj;
		}

		public Expression CreateFormatDelegate(Expression expression, ExpressionType? parentExpressionType)
		{
			return expression;
		}
	}
}